/**
 * Copyright (c) 2015-2017, Henry Yang 杨勇 (gismail@foxmail.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mapfinal.portal.web.tag;

import java.io.IOException;
import java.util.Map;

import com.mapfinal.portal.model.ActionLog;
import com.mapfinal.portal.service.ActionLogService;
import com.lambkit.common.util.StringUtils;
import com.lambkit.common.service.ServiceKit;
import com.lambkit.web.tag.base.BaseDirectiveModel;
import com.jfinal.kit.StrKit;
import com.jfinal.render.FreeMarkerRender;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

/**
 * @author yangyong 
 * @website: www.lambkit.com
 * @email: gismail@foxmail.com
 * @date 2019-05-23
 * @version 1.0
 * @since 1.0
 */
/**
 * cms_action_log标签<br>
 * 参数：{id:主键}
 * 返回值：{entity:cms_action_log信息}
 * @author lambkit
 */
public class ActionLogDirective extends BaseDirectiveModel {

	@Override
	public void executeMe(Environment env, Map params, TemplateModel[] loopVars,
			TemplateDirectiveBody body) throws TemplateException, IOException {
		// TODO Auto-generated method stub
		String id = get(params, "id");
		String userId = get(params, "user_id");
		String anonym = get(params, "anonym");
		String actionKey = get(params, "action_key");
		String actionQuery = get(params, "action_query");
		String actionName = get(params, "action_name");
		String source = get(params, "source");
		String medium = get(params, "medium");
		String campaign = get(params, "campaign");
		String content = get(params, "content");
		String term = get(params, "term");
		String ip = get(params, "ip");
		String agent = get(params, "agent");
		String referer = get(params, "referer");
		String se = get(params, "se");
		String sek = get(params, "sek");
		String deviceId = get(params, "device_id");
		String platform = get(params, "platform");
		String system = get(params, "system");
		String brand = get(params, "brand");
		String model = get(params, "model");
		String network = get(params, "network");
		String created = get(params, "created");
		int pagenum = getInt(params, "pagenum", 0);
		int pagesize = getInt(params, "pagesize", 0);
		String wheresql = get(params, "sql", null);
		String sql = " from cms_action_log where "; 
		if(wheresql == null) {
			sql += " 1=1 ";
			if(StringUtils.hasText(id)) sql += " and id like '%" + id + "%'";//varchar
			if(StringUtils.hasText(userId)) sql += " and user_id=" + userId;//int8
			if(StringUtils.hasText(anonym)) sql += " and anonym like '%" + anonym + "%'";//varchar
			if(StringUtils.hasText(actionKey)) sql += " and action_key like '%" + actionKey + "%'";//varchar
			if(StringUtils.hasText(actionQuery)) sql += " and action_query like '%" + actionQuery + "%'";//varchar
			if(StringUtils.hasText(actionName)) sql += " and action_name like '%" + actionName + "%'";//varchar
			if(StringUtils.hasText(source)) sql += " and source like '%" + source + "%'";//varchar
			if(StringUtils.hasText(medium)) sql += " and medium like '%" + medium + "%'";//varchar
			if(StringUtils.hasText(campaign)) sql += " and campaign like '%" + campaign + "%'";//varchar
			if(StringUtils.hasText(content)) sql += " and content like '%" + content + "%'";//varchar
			if(StringUtils.hasText(term)) sql += " and term like '%" + term + "%'";//varchar
			if(StringUtils.hasText(ip)) sql += " and ip like '%" + ip + "%'";//varchar
			if(StringUtils.hasText(agent)) sql += " and agent like '%" + agent + "%'";//varchar
			if(StringUtils.hasText(referer)) sql += " and referer like '%" + referer + "%'";//varchar
			if(StringUtils.hasText(se)) sql += " and se like '%" + se + "%'";//varchar
			if(StringUtils.hasText(sek)) sql += " and sek like '%" + sek + "%'";//varchar
			if(StringUtils.hasText(deviceId)) sql += " and device_id like '%" + deviceId + "%'";//varchar
			if(StringUtils.hasText(platform)) sql += " and platform like '%" + platform + "%'";//varchar
			if(StringUtils.hasText(system)) sql += " and system like '%" + system + "%'";//varchar
			if(StringUtils.hasText(brand)) sql += " and brand like '%" + brand + "%'";//varchar
			if(StringUtils.hasText(model)) sql += " and model like '%" + model + "%'";//varchar
			if(StringUtils.hasText(network)) sql += " and network like '%" + network + "%'";//varchar
			if(StringUtils.hasText(created)) sql += " and created like '%" + created + "%'";//timestamp
		} else {
			sql += wheresql;
		}
		
		String orderby = get(params, "orderby", null);
		
		if(StrKit.notBlank(orderby)) {
			sql += " order by " + orderby;
		}
		
		ActionLogService service = ActionLog.service();
		
		String tagEntityKeyname = get(params, "key", "entity");
		if(pagenum==0) {
			env.setVariable(tagEntityKeyname, FreeMarkerRender.getConfiguration().getObjectWrapper().wrap(
					service.dao().findFirst("select *" + sql)));
		} else {
			if(pagesize==0) {
				env.setVariable(tagEntityKeyname, FreeMarkerRender.getConfiguration().getObjectWrapper().wrap(
						service.dao().find("select *" + sql)));
			} else {
				env.setVariable(tagEntityKeyname, FreeMarkerRender.getConfiguration().getObjectWrapper().wrap(
						service.dao().paginate(pagenum, pagesize, "select *", sql)));
			}
		}
        body.render(env.getOut());
	}
}
